import regeneratorRuntime from '../../../../resource/libs/runtime'
import service from '../../../../common/service'

Page({
  data: {
    id: '',
    choose: '',
    list: ['没有司机接单', '等待司机太久', '司机不接电话', '暂时不想要司机'],
    feedback: '',
    feedback_num: 0,
    loading: false
  },

  onLoad (options) {
    this.setData({
      id: options.id
    })
  },

  chooseTap ({ currentTarget }) {
    let index = currentTarget.dataset.index
    let list = this.data.list
    let choose = list[index]
    let old_choose = this.data.choose
    if (choose === old_choose) {
      choose = ''
    }
    this.setData({ choose })
  },

  feedbackInput ({ detail }) {
    let value = detail.value
    this.setData({
      feedback: value,
      feedback_num: value.length
    })
  },

  async submit () {
    if (this.data.loading) { return }
    this.setData({ loading: true })
    let choose = this.data.choose
    let feedback = this.data.feedback
    let res = await service.postPromise('order/cancel', {
      id: this.data.id,
      remark: choose ? choose + '，' + feedback : feedback
    })
    this.setData({ loading: false })
    if (!res) { return }
    if (typeof res === 'number') {
      wx.showModal({
        title: '取消失败',
        showCancel: false,
      })
      return
    }
    wx.navigateBack({
      delta: 2
    })
  }
})